<?php
class MaterialAction extends BaseAction
{
    public function _initialize()
    {
        Vendor('PHPThumb.Thumb');
        parent::_initialize();
    }

    public function index()
    {
    }

    /**
     * 素材库网站图片维护
     */
    public function slide()
    {
        $ename = $this->_get('ename', '');
        if (empty($ename)) {
            return;
        }
        $brand = D('Brand')->getOneByEname($ename, 'id, name, ename, initials');
        if (empty($brand)) {
            $thumb = PhpThumbFactory::create(SITE_PATH . '/static/img/noslide.png');
            $thumb->show();
        }
        $this->getSeasonImage($brand);
    }

    /**
     *
     * 获取对应目录下显得品牌展示图是否存在
     * 3～5月为春季  spring
     * 6～8月为夏季  summer
     * 9～11月为秋季 autumn
     * 12～2月为冬季 winter
     * 如果四季都没有图片就显示默认图片一张
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getSeasonImage($brand)
    { //获取当前季度
        $debug = intval($this->_get('debug'));
        $isHave = false;
        $season = $this->getSeason();
        $brandPath = UPLOAD_PATH . 'material-slide/' . $brand['initials'] . '-' . $brand['name'];
        $defaultImg = SITE_PATH . '/static/img/noslide.png';
        //window 进行字符转换 适应中文环境
        if (IS_WIN) {
            $brandPath = auto_charset($brandPath, 'utf-8', 'gbk');
        }
        if ($debug) {
            dump($brand);
            dump(auto_charset($brandPath));
            dump(is_dir($brandPath));
        }
        if (is_dir($brandPath)) {
            //季节信息获取
            $filePath = null;
            //循环获取季节信息最多进行3次
            for ($i = 1; $i <= 4; $i++) {
                if ($i != 1) {
                    $season = $this->getSeason(null, -3 * ($i - 1));
                }
                $seasonPath = $brandPath . '/' . $season;
                if ($debug) {
                    dump(auto_charset($seasonPath));
                    dump(is_dir($seasonPath));
                }
                if (is_dir($seasonPath)) {
                    $files = $this->getFiles($seasonPath);
                    if (!empty($files)) {
                        $fileKey = array_rand($files, 1);
                        $filePath = $seasonPath . '/' . $files[$fileKey];
                        if (is_file($filePath)) {
                            $isHave = true;
                            break;
                        } else {
                            $isHave = false;
                        }
                    }
                }
            }
        }
        if (!$isHave) {
            $seasonPath = $brandPath . '/common';
            if (is_dir($seasonPath)) {
                $files = $this->getFiles($seasonPath);
                if (!empty($files)) {
                    $fileKey = array_rand($files, 1);
                    $filePath = $seasonPath . '/' . $files[$fileKey];

                }
            }
        }
        //获取图片
        if (!empty($filePath) && $isHave) {
        } else {
            $filePath = $defaultImg;
        }
        if ($debug) {
            dump($filePath);
        } else {
            $thumb = PhpThumbFactory::create($filePath);
            $thumb->show();
        }
    }

    /**
     * 获取当前季节
     * @param null $month
     * @param int $count
     * @return mixed
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getSeason($month = null, $count = 0)
    {
        if (empty($month)) {
            $month = intval(date('n'));
        }
        if (!empty($count)) {
            $date = new Date();
            $dayTime = $date->format('%Y-%m-%d');
            $month = $date->dateAdd($count, 'm')->format('%m');
            $month = intval($month);
        }
        $months = array();
        $months[1] = 'winter';
        $months[2] = 'winter';
        $months[3] = 'spring';
        $months[4] = 'spring';
        $months[5] = 'spring';
        $months[6] = 'summer';
        $months[7] = 'summer';
        $months[8] = 'summer';
        $months[9] = 'autumn';
        $months[10] = 'autumn';
        $months[11] = 'autumn';
        $months[12] = 'winter';
        return $months[$month];
    }

    /**
     * 获取制定文件夹下的文件
     * @param $path
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getFiles($path)
    {
        $files = array();
        if (is_dir($path)) {
            $handler = opendir($path);
            while (($filename = readdir($handler)) !== false) {
                if ($filename != '.' && $filename != '..' && $filename != '.svn') {
                    if (!empty($filename)) {
                        $files[] = $filename;
                    }
                }
            }
            closedir($handler);
        }
        return $files;
    }
}

?>